package com.lms.netty.protobuf;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-06-18 11:14
 * @Description:
 */
public class MyDataInfoHandler extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {
        MyDataInfo.MyMessage.DataType type = msg.getType();

        if (type== MyDataInfo.MyMessage.DataType.StudentType){
            MyDataInfo.Student student = msg.getStudent();
            System.out.println("学生：id:"+student.getId()+",name:"+student.getName());

        }else if(type== MyDataInfo.MyMessage.DataType.TeacherType){
            MyDataInfo.Teacher teacher = msg.getTeacher();
            System.out.println("老师：id:"+teacher.getId()+",name:"+teacher.getName()+",job:"+teacher.getJob());

        }else {
            System.out.println("传输的数据类型不对！");
        }

    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端", CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(cause.getMessage());
        ctx.close();
    }
}
